package cn.tvplaza.shakeclub;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.tvplaza.shakeclub.community.CommunityFragment;
import cn.tvplaza.shakeclub.mine.MineFragment;
import cn.tvplaza.shakeclub.shakermb.HomeShakeFragment;
import cn.tvplaza.shakeclub.video.LiveMainFragment;
import cn.tvplaza.shakeclub.video.view.HeartView;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Bind(R.id.main_heartview)
    HeartView mHeartView;

    @Bind(R.id.drawer_layout)
    DrawerLayout drawer;

    @Bind(R.id.iv_live_img)
    ImageView indexBImg;
    @Bind(R.id.iv_community)
    ImageView indexCImg;
    @Bind(R.id.iv_mine)
    ImageView indexDImg;

    @Bind(R.id.tv_live)
    TextView indexBTag;
    @Bind(R.id.tv_community)
    TextView indexCTag;
    @Bind(R.id.tv_mine)
    TextView indexDTag;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private static Index CURRENT_INDEX = Index.HOME;

    private enum Index {
        HOME, LIVE, COMMUNITY, MINE
    }

    // 主页
    private HomeShakeFragment mHomeFragment;
    // 电摇
    private LiveMainFragment mLiveMainFragment;
    // 社群
    private CommunityFragment mCommunityFragment;
    // 我的
    private MineFragment mMineFragment;

    private int selectedColor;
    private HeartFloating mHeartFloatAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        fragmentManager = getSupportFragmentManager();
        //默认选择 主页
        selectBottom(Index.HOME, false);
        selectedColor = getResources().getColor(R.color.main_color);
//        indexATag.setTextColor(selectedColor);
//        indexAImg.setImageResource(R.mipmap.icon_main_home_selec);
        mHeartFloatAnim = new HeartFloating();
    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    private class HeartFloating implements Runnable {
        @Override
        public void run() {
            mHeartView.addHeart();
//            Log.e("heart", "float");
            mHandler.postDelayed(this, 100);
        }
    }

    @OnClick({R.id.ll_shake_shake_btn, R.id.ll_main_tipoff, R.id.ll_main_community, R.id.ll_main_mine})
    public void switchFragment(View v) {
        switch (v.getId()) {
            case R.id.ll_shake_shake_btn:
                if (CURRENT_INDEX == Index.HOME) {
                    return;
                }
                selectBottom(Index.HOME, false);
//                indexAImg.setImageResource(R.mipmap.icon_main_home_selec);
//                indexATag.setTextColor(selectedColor);
                break;
            case R.id.ll_main_tipoff:
                if (CURRENT_INDEX == Index.LIVE) {
                    return;
                }
                selectBottom(Index.LIVE, false);
                indexBImg.setImageResource(R.mipmap.ic_launcher);
                indexBTag.setTextColor(selectedColor);

                break;
            case R.id.ll_main_community:
                if (CURRENT_INDEX == Index.COMMUNITY) {
                    return;
                }
                selectBottom(Index.COMMUNITY, false);
                indexCImg.setImageResource(R.mipmap.ic_launcher);
                indexCTag.setTextColor(selectedColor);
                break;
            case R.id.ll_main_mine:
                if (CURRENT_INDEX == Index.MINE) {
                    return;
                }
                selectBottom(Index.MINE, false);
                indexDImg.setImageResource(R.mipmap.ic_launcher);
                indexDTag.setTextColor(selectedColor);
                break;
        }

    }


    /**
     * 选择fragment
     *
     * @param index
     */
    private void selectBottom(Index index, boolean addToStack) {
        transaction = fragmentManager.beginTransaction();
        //全部出栈
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        hideAllFragment(transaction);
        unSelectedAllTagsAndPics();

        Fragment fragment = getFragment(index);
        if (fragment != null) {
            if (!fragment.isAdded()) {
                transaction.add(R.id.fl_frame_container, fragment);
            }
            transaction.show(fragment);
        }
        if (addToStack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();

        //标记当前index
        CURRENT_INDEX = index;

        // 替换其他标志
        switchState(index);
    }

    // customer
    private void switchState(Index index) {

        switch (index) {
            case HOME:      //
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);  // 开启手势滑动
                mHandler.removeCallbacks(mHeartFloatAnim);
                break;
            case LIVE:      //
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);  // 禁止手势滑动
                // heart Anim
                mHandler.postDelayed(mHeartFloatAnim, 100);
                break;
            case COMMUNITY:      //
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);  // 禁止手势滑动
                mHandler.removeCallbacks(mHeartFloatAnim);
                break;
            case MINE:      //
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);  // 禁止手势滑动
                mHandler.removeCallbacks(mHeartFloatAnim);
                break;
            default:
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);  // 开启手势滑动
                mHandler.removeCallbacks(mHeartFloatAnim);
                break;
        }
    }

    private void hideAllFragment(FragmentTransaction transaction) {
        // 此处添加自己的fragment...
        hideFragment(transaction, mHomeFragment);
        hideFragment(transaction, mLiveMainFragment);
        hideFragment(transaction, mMineFragment);
        hideFragment(transaction, mCommunityFragment);
    }

    /**
     * 取消选中所有导航按钮
     */
    private void unSelectedAllTagsAndPics() {
        int tempColor = getResources().getColor(R.color.main_gray_text_color);
//        indexATag.setTextColor(tempColor);
//        indexBTag.setTextColor(tempColor);
//        //        indexCTag.setTextColor(defaultIndexTextColors);
//        indexDTag.setTextColor(tempColor);
//        indexETag.setTextColor(tempColor);
//
//        indexAImg.setImageResource(R.mipmap.icon_main_home);
//        indexBImg.setImageResource(R.mipmap.icon_main_live);
//        //        indexCImg.setImageResource(R.mipmap.y_crmweixuanzhong);
//        indexDImg.setImageResource(R.mipmap.icon_main_tipoff);
//        indexEImg.setImageResource(R.mipmap.icon_main_mine);
    }

    /**
     * 隐藏fragment
     *
     * @param transaction
     * @param fragment
     */
    private void hideFragment(FragmentTransaction transaction, Fragment fragment) {
        if (fragment != null) {
            transaction.hide(fragment);
        }
    }

    /**
     * 获取fragment
     *
     * @param index
     * @return
     */
    private Fragment getFragment(Index index) {
        Fragment fragment = null;
        switch (index) {
            case HOME:      //首页
                if (mHomeFragment == null) {
                    mHomeFragment = HomeShakeFragment.newInstance();
                }
                fragment = mHomeFragment;
                break;
            case LIVE:     //电摇
                if (mLiveMainFragment == null) {
                    mLiveMainFragment = LiveMainFragment.newInstance();
                }
                fragment = mLiveMainFragment;
                break;
            case COMMUNITY:       // 社群
                if (mCommunityFragment == null) {
                    mCommunityFragment = CommunityFragment.newInstance();
                }
                fragment = mCommunityFragment;
                break;
            case MINE:      //我的
                if (mMineFragment == null) {
                    mMineFragment = MineFragment.newInstance();
                }
                fragment = mMineFragment;
                break;
            default:
                if (mHomeFragment == null) {
                    mHomeFragment = HomeShakeFragment.newInstance();
                }
                fragment = mHomeFragment;
                break;
        }
        return fragment;
    }

    @Override
    public void onBackPressed() {
//        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {

        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
